#下载百度图片  
from urllib.request import *  
import itertools  
import urllib.parse  
import re  
  
str_table = {  
    '_z2C$q': ':',  
    '_z&e3B': '.',  
    'AzdH3F': '/'  
}  
char_table = {  
    'w': 'a','k': 'b','v': 'c','1': 'd','j': 'e','u': 'f',  
    '2': 'g','i': 'h','t': 'i','3': 'j','h': 'k','s': 'l',  
    '4': 'm','g': 'n','5': 'o','r': 'p','q': 'q','6': 'r',  
    'f': 's','p': 't','7': 'u','e': 'v','o': 'w','8': '1',  
    'd': '2','n': '3','9': '4','c': '5','m': '6','0': '7',  
    'b': '8','l': '9','a': '0'  
}  
char_table = {ord(key): ord(value) for key, value in char_table.items()}  
  
reg=re.compile("ippr_z2C\$qAzdH3FAzdH3F[^\"]+_z\&e3B3r2")  
urls=[]  
  
def burls(term):  
    word=urllib.parse.quote(term)  
    url="http://image.baidu.com/search/acjson?tn=resultjson_com\
&ipn=rj&ct=201326592&fp=result&queryWord={0}&cl=2&lm=-1&ie=utf-8\
&oe=utf-8&st=-1&ic=0&word={0}&face=0&istype=2nc=1&pn={1}&rn=60"  
    for x in range(0,100000,60):  
        urls.append(url.format(word,x))  
      
def decode(url):  
    for key, value in str_table.items():  
        url = url.replace(key, value)  
    return url.translate(char_table)  
  
def downLoad(term):  
    print('开始下载......')  
    burls(term)  
    i=0  
    for url in urls:  
        html=urlopen(url).read().decode('utf-8')  
        links=re.findall(reg,html)  
  
        for link in links:  
            link=decode(link)  
  
            path='E:/python/%d.jpg'%i  
            try:  
                urlretrieve(link,path)  
            except:  
                print('失败',end='\n')  
            else:  
                print('下载第%d张'%i,link)  
                i+=1  
  
term=input('请输入要搜索的对象：')  
downLoad(term)  
